`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Module: AUDIO_KEYBOARD                                                       //
// Author: Zhiyuan Lin                                                          //
// Date: 4/26/2014                                                              //
// Description: This module is to use the keyboard as input.                    //
//////////////////////////////////////////////////////////////////////////////////
module AUDIO_KEYBOARD(
    input wire K_CLK, //Clock pin form keyboard 
    input wire Data, //Data pin form keyboard 
	 	 
	 output reg S_Pause_SPACE,
    output reg S_Start_0,
    output S_Song1_1,
	 output S_Song2_2,
	 output S_Song3_3,
	 output reg S_Turbo_4,
	 
	 output reg B_Restart_R,
	 output reg B_QuitPause_ESC,
	 output reg B_QuitOver_Q,
	 output reg B_Enter_ENTER,
	 output reg B_EnterGame_M,
	 
    output reg B_Btn1_Z,
    output reg B_Btn2_X,
    output reg B_Btn3_C,
    output reg B_Btn4_V
	  
	 );
	 
    reg Start, Pause, Turbo;
    reg [2:0] Select;
    
    assign S_Song1_1 = Select[0];
    assign S_Song2_2 = Select[1];  	 
	 assign S_Song3_3 = Select[2];

    reg [7:0] Data_Curr, Data_Past; 
    reg [3:0] b; 
    reg FLAG; 
	 	 
 
always @(negedge K_CLK) //Activating at negative edge of clock from keyboard 
  begin 
  case(b) 
	 1:; //first bit 
	 2:Data_Curr[0] <= Data; 
	 3:Data_Curr[1] <= Data; 
	 4:Data_Curr[2] <= Data; 
	 5:Data_Curr[3] <= Data; 
	 6:Data_Curr[4] <= Data; 
	 7:Data_Curr[5] <= Data; 
	 8:Data_Curr[6] <= Data; 
	 9:Data_Curr[7] <= Data; 
	 10:FLAG<=1; //Parity bit 
	 11:FLAG<=0; //Ending bit 								
  endcase 
  if(b <= 10) 		
	  b <= b + 1; 
  else 
	  b <= 1;			
end 

always@(posedge FLAG)
  begin			
  if (Data_Past == 8'hF0) 
    begin		
      case (Data_Curr)
//	   8'h16: Song_1 	 <= 0;
//	   8'h1E: Song_2 	 <= 0;
//		8'h26: Song_3 	 <= 0;
	   8'h45: Start 	 <= 0;	
	   8'h29: Pause    <= 0;
	   8'h25: Turbo    <= 0;
		
		
		8'h2D: B_Restart_R   	<= 0;
		8'h1A: B_Btn1_Z 			<= 0;
		8'h22: B_Btn2_X 			<= 0;
		8'h21: B_Btn3_C 			<= 0;
		8'h2A: B_Btn4_V 			<= 0;
      8'h76: B_QuitPause_ESC 	<= 0;
      8'h15: B_QuitOver_Q 		<= 0;
      8'h5A: B_Enter_ENTER 	<= 0;
		8'h3A: B_EnterGame_M    <= 0;
		
	   endcase															
	   Data_Past <= Data_Curr;
  end
  else 
    begin
	 
	 
	 
//		if (Data_Curr == 8'h16) //1 for Song1
//		  begin
//		  Song_1 <= 1;							
//		  Data_Past <= Data_Curr;
//		end
//		
//		else if (Data_Curr == 8'h1E) //2 for Song2
//		  begin							
//		  Song_2 <= 1;							
//		  Data_Past <= Data_Curr;					
//		end	
//		
//		else if (Data_Curr == 8'h26) //3 for Song3
//		  begin
//          Song_3 <= 1;		     											
//			 Data_Past <= Data_Curr;
//		end
//		
		
		if (Data_Curr == 8'h16) //1 for Song1
		  begin
		  Select <= 001;							
		  Data_Past <= Data_Curr;
		end
		
		else if (Data_Curr == 8'h1E) //2 for Song2
		  begin							
		  Select <= 010;							
		  Data_Past <= Data_Curr;					
		end	
		
		else if (Data_Curr == 8'h26) //3 for Song3
		  begin
          Select <= 100;		     											
			 Data_Past <= Data_Curr;
		end
		
		
		
		
		
		else if (Data_Curr == 8'h45) //S for Start
		  begin							
		    Start <= 1;							
			 Data_Past <= Data_Curr;
		end
		
		else if (Data_Curr == 8'h29) //SPACE for Pause
		  begin	
        Pause <= 1;		   
		  Data_Past <= Data_Curr;
		end
		
		else if (Data_Curr == 8'h25) //4 for Turbo
		  begin							
		  Turbo <= 1;
		  Data_Past <= Data_Curr;
		end
		
		else if (Data_Curr == 8'h2D) //R for Restart
		  begin							
		  B_Restart_R <= 1;
		  Data_Past <= Data_Curr;
		end
		
		else if (Data_Curr == 8'h1A) //Z for Btn1
		  begin							
		  B_Btn1_Z <= 1;
		  Data_Past <= Data_Curr;
		end
		
		else if (Data_Curr == 8'h22) //X for Btn2
		  begin							
		  B_Btn2_X <= 1;
		  Data_Past <= Data_Curr;
		end
		
		else if (Data_Curr == 8'h21) //C for Btn3
		  begin							
		  B_Btn3_C <= 1;
		  Data_Past <= Data_Curr;
		end
		
		else if (Data_Curr == 8'h2A) //V for Btn4
		  begin							
		  B_Btn4_V <= 1;
		  Data_Past <= Data_Curr;
		end
		
		else if (Data_Curr == 8'h76) //ESC for PauseState Quit
		  begin							
		  B_QuitPause_ESC <= 1;
		  Data_Past <= Data_Curr;
		end
		
		else if (Data_Curr == 8'h15) //Q for OverState Quit
		  begin							
		  B_QuitOver_Q <= 1;
		  Data_Past <= Data_Curr;
		end
		
		else if (Data_Curr == 8'h5A) //ENTER for StartState Enter
		  begin							
		  B_Enter_ENTER <= 1;
		  Data_Past <= Data_Curr;
		end
		
		else if (Data_Curr == 8'h3A) //M for Game Enter
		  begin							
		  B_EnterGame_M <= 1;
		  Data_Past <= Data_Curr;
		end
	
		else
		  begin 							
		  Data_Past <= Data_Curr;
		end			
  end
end		

//////////////////////////////////////////////////
//Configure Keys as Switches                    //
//////////////////////////////////////////////////	
  	
/*always@(posedge Song_1)	
	begin				
	  S_Song1_1 <= S_Song1_1 + 1;
//	  S_Song2_2 <= 0;
//	  S_Song3_3 <= 0;
	end

always@(posedge Song_2)
	begin			
	  S_Song2_2 <= S_Song2_2 + 1;
//	  S_Song1_1 <= 0;
//	  S_Song3_3 <= 0;
	end
	
always@(posedge Song_3)
	begin
	  S_Song3_3 <= S_Song3_3 + 1;
//	  S_Song2_2 <= 0;
//	  S_Song1_1 <= 0;
	end
	*/
always@(posedge Start)
	begin	
	  S_Start_0 <= S_Start_0 + 1;
	end
	
always@(posedge Pause)
	begin			
	  S_Pause_SPACE <= S_Pause_SPACE + 1;
	end
	
always@(posedge Turbo)
	begin			
	  S_Turbo_4 <= S_Turbo_4 + 1;
	end

endmodule		